#!/bin/bash

######################### CONTROL TCP CHANNEL PARAMS #########################
## Control source IP address
CTRL_SRC_IP=10.1.2.30
## Control destination IP address
CTRL_DST_IP=10.1.2.31
## Control destination TCP port
CTRL_DST_PORT=8000
## Verbosity level
VLEVEL=4
## Number of transmission runs
NRUNS=20
## This is a multiplier that approssimate the 
## number of reception runs to be performed.
## Note that if you run the transmitter 5 times
## you sould assume to receive less packets than
## the transmitted ones. Therefore keep this value
## below the one chosen for $NRUNS
RXROUNDS=10
## Guard time interval period before to 
## restart the cycle of transmissions
RX_RELOAD_SLEEP=0 #s
## Guard inteval before to restart the cycle 
## of transmissions.
TX_RELOAD_SLEEP=$RX_RELOAD_SLEEP #s

########################### RECEIVER CMDS PARAMS ###########################
## Receiver's script filename
RX_SCRIPT=receiver.sh
RX_FILENAME=rxcmds
DATA_RX_SCRIPT=data_receive.sh
## Receiver data stream interface name
RXIFACE=ath0
## Specify details about the L2 and below link
RXL2SPECS=wlan
## Results filename
DATA_FILENAME=data.m
## Matrix name
CELL=mtrx

########################### TRANSMITTER CMDS PARAMS #########################
## Transmitter's script filename
TX_SCRIPT=transmitter.sh
TX_FILENAME=txcmds
## Transmitter's used interface
TXIFACE=ath0
## Transmitter data port
INPORT=2424
## Receiver data port
OUTPORT=12345
## Source IP address of the transmitter
SRC_IP=10.1.2.30
## Destination IP address
DST_IP=255.255.255.255
## Transmission mode [bc = broadcast / uc = unicast]
TXMODE=bc
## Transport type
TRANSPORT=udp
## Specify details about the L2 and below link
TXL2SPECS=wlan
## Packet size
SIZE=100 #B
## Inter packet latency used by the sender [us]. 
## This value is not very accurate: when used
## the minimum introduced latency can be greate 
## than the expected one, despite the imposed value.
## This parameter should be tipically set to 0 and 
## used only when needing a concrete latency value.
LAT=10 #us
## number of packets we want to transmit per 
## "send" command try.
PKTS=10
## Hereby we specify the data as static 
## (non random) and with additional sequence number
DATA_TYPE="static sequence wlanchan"

## Here we specify the bitrates we want to use 
## (eg: "BITRATE=( 1 5.5 11 ); BITRATE_N=3" )
declare -a BITRATE
BITRATE=( 1 )
BITRATE_N=1
## Here the channels used are specified 
## (eg: "CHANNELS=( 1 2 3 4 5 6 7 11 12 13 ); 
## (CHANNELS_N=10" )
declare -a CHANNELS
CHANNELS=( 1 2 3 4 5 6 7 8 9 10 11 12 13 )
CHANNELS_N=13

######################### TRANSMITTER COMMANDS GENERATION #########################
echo -e "openctrl srcip=$CTRL_SRC_IP dstip=$CTRL_DST_IP dstport=$CTRL_DST_PORT"> $TX_FILENAME
echo -e "opendata $TRANSPORT iface=$TXIFACE $TXMODE port=$INPORT $TXL2SPECS">> $TX_FILENAME

ctr1=0;
while [ $ctr1 -lt $CHANNELS_N ]; do
  ctr0=0;
  while [ $ctr0 -lt $BITRATE_N ]; do
    echo -e "set rate=${BITRATE[${ctr0}]}Mb" >> $TX_FILENAME
    echo -e "set channel=${CHANNELS[${ctr1}]}" >> $TX_FILENAME
    echo -e "sleep 100000" >> $TX_FILENAME
    echo -e "send ip=$DST_IP port=$OUTPORT lat=$LAT npkts=$PKTS sz=$SIZE $DATA_TYPE" >> $TX_FILENAME
    #echo -e "sleep 1" >> $TX_FILENAME
    let ctr0=ctr0+1
  done
  let ctr1=ctr1+1
done
echo -e "closedata" >> $TX_FILENAME
echo -e "closectrl" >> $TX_FILENAME
chmod +x $TX_FILENAME

########################### RECEIVER COMMANDS GENERATION ##########################
echo -e "open ctrl_ip=$CTRL_DST_IP ctrl_port=$CTRL_DST_PORT data_ip=$CTRL_DST_IP $RXL2SPECS"> $RX_FILENAME

########################## TRANSMITTER SCRIPT GENERATION ##########################
#Adding 1 second after the receiver have been reloaded before the transmitter 
#starts transmitting again (to be safe).
let TX_RELOAD_SLEEP=TX_RELOAD_SLEEP+1

echo -e "#!/bin/bash\n" >$TX_SCRIPT
echo -e "ctr0=0">> $TX_SCRIPT
echo -e "while [ \$ctr0 -lt $NRUNS ]; do">> $TX_SCRIPT
echo -e "  ./testnet -t $TX_FILENAME -v $VLEVEL">> $TX_SCRIPT
echo -e "  sleep $TX_RELOAD_SLEEP">> $TX_SCRIPT
echo -e "  let ctr0=ctr0+1">> $TX_SCRIPT
echo -e "done">> $TX_SCRIPT
chmod +x $TX_SCRIPT

######################### CTRL RECEIVER SCRIPT GENERATION #########################
echo -e "#!/bin/bash\n" >$RX_SCRIPT
echo -e "ctr0=0">> $RX_SCRIPT
echo -e "while [ \$ctr0 -lt $NRUNS ]; do">> $RX_SCRIPT
echo -e "  ./testnet -r $RX_FILENAME -v $VLEVEL">> $RX_SCRIPT
echo -e "  sleep $RX_RELOAD_SLEEP">> $RX_SCRIPT
echo -e "  let ctr0=ctr0+1">> $RX_SCRIPT
echo -e "done">> $RX_SCRIPT
chmod +x $RX_SCRIPT

######################### DATA RECEIVER SCRIPT GENERATION #########################
echo -e "#!/bin/bash" >$DATA_RX_SCRIPT
echo -e "if [ \$1 != \"\" ]; then" >> $DATA_RX_SCRIPT
echo -e "  DATA_FILENAME=\$1" >> $DATA_RX_SCRIPT
echo -e "else" >> $DATA_RX_SCRIPT
echo -e "  echo \"Defaulting saved data filename to $DATA_FILENAME\"" >> $DATA_RX_SCRIPT
echo -e "  DATA_FILENAME=$DATA_FILENAME" >> $DATA_RX_SCRIPT
echo -e "fi;" >> $DATA_RX_SCRIPT
echo -e "echo -e \"%% Received pkts Sequence number & Tx channel\" > \$DATA_FILENAME" >> $DATA_RX_SCRIPT
echo -e "echo -e \"mtrx=[\" > \$DATA_FILENAME" >> $DATA_RX_SCRIPT
TOTPKTS=$[$CHANNELS_N * $PKTS * $RXROUNDS];
echo -e "tcpdump -i ath0 -X 'port $OUTPORT' -c $TOTPKTS  |awk '\$1 == \"0x0010:\" {printf(\"%s\",\$9); }; \$1 == \"0x0020:\" {printf(\" hex2dec(\\\\47%s\\\\47) / 256\\\\n\", \$2);}' >> \$DATA_FILENAME" >> $DATA_RX_SCRIPT
echo -e "echo -e \"];\" >> \$DATA_FILENAME" >> $DATA_RX_SCRIPT
# echo -e "#!/bin/bash" >$DATA_RX_SCRIPT 
# echo -e "echo -e \"%% RX_PKTS=$RX_PKTS - SIZE=$SIZE\" > $DATA_FILENAME" >> $DATA_RX_SCRIPT
# ctr1=0;
# while [ $ctr1 -lt $CHANNELS_N ]; do
#   echo "iwconfig $RXIFACE channel ${CHANNELS[${ctr1}]}" >> $DATA_RX_SCRIPT
#   ctr0=0;
#   while [ $ctr0 -lt $BITRATE_N ]; do
#     echo "echo -e \" SWITCHING TO CHANNEL ${CHANNELS[${ctr1}]} - \t BITRATE = ${BITRATE[${ctr0}]}Mb\"" >> $DATA_RX_SCRIPT
#     echo "echo -e \"%% CHANNEL = ${CHANNELS[${ctr1}]} - \t BITRATE = ${BITRATE[${ctr0}]}Mb\" >> $DATA_FILENAME" >> $DATA_RX_SCRIPT
#     #Definisco la matrice di matlab
#     echo "echo -e \"ch${CHANNELS[${ctr1}]}_br${BITRATE[${ctr0}]}=[\"|awk '{sub(\"5.5\",\"5_5\",\$1); print}' >> $DATA_FILENAME" >> $DATA_RX_SCRIPT
#     #echo "echo -e \"$MATRIX($ctr1+1,$ctr0+1,:,:)=[\"|awk '{sub(\"5.5\",\"5_5\",\$1); print}' >> $DATA_FILENAME" >> $RX_SCRIPT
#     #COMMAND TO EXECUTE
#     #tcpdump -i ath0 -l 'port 12345' -c 5 -XX|awk '$3 == "tsft" 
#     #{sub("us","",$2); printf("%s ",$2)} $1 == "0x0050:" 
#     #{printf("%s%s\n", $5, $6)}'|awk '{ printf("%s %s\n", $2, $1) }'
#     #tcpdump -i ath0 -l 'port 12345' -c 30 -XX|awk '$3 == "tsft"
#     #{print $1}' |awk '{sub(":","",$1);sub(":","",$1);print}'
# 
#     echo "tcpdump -i $RXIFACE -l 'port $INPORT' -c $RX_PKTS -XX|awk '\$3 == \"tsft\" {sub(\":\",\"\",\$1); sub(\":\",\"\",\$1); printf(\"%s \", substr(\$1,5))}; \$1 == \"0x0020:\" {printf(\"0x%s%s\n\", \$7, \$8)}'|awk '{ printf(\"%s\n\", strtonum(\$1)) }' >> $DATA_FILENAME" >> $DATA_RX_SCRIPT
#     echo "echo -e \"];\" >> $DATA_FILENAME" >> $DATA_RX_SCRIPT
#     echo "echo -e \"$CELL{$ctr1+1,$ctr0+1} = ch${CHANNELS[${ctr1}]}_br${BITRATE[${ctr0}]};\"|awk '{sub(\"5.5\",\"5_5\",\$3); print}' >> $DATA_FILENAME" >> $DATA_RX_SCRIPT 
#     echo -e "sleep $RX_INTERDELAY" >> $DATA_RX_SCRIPT
#     let ctr0=ctr0+1
#   done
#   let ctr1=ctr1+1
# done
# chmod +x $DATA_RX_SCRIPT 












